<?php

require_once 'recaptcha/recaptchalib.php';

class CI_Recaptcha {

    private $_last_error = null;
    private $_response = null;
    private $_ci_instance = null;

    public function __construct() {
        $this->_ci_instance = & get_instance();
    }

    public function get_html() {
        return recaptcha_get_html(RECAPTCHA_PUBLIC_KEY, $this->get_error());
    }

    public function check() {
        $this->_response = recaptcha_check_answer(RECAPTCHA_PRIVATE_KEY,
                        $this->_ci_instance->input->ip_address(),
                        $this->_ci_instance->input->post("recaptcha_challenge_field"),
                        $this->_ci_instance->input->post("recaptcha_response_field")
        );

        return $this->_response->is_valid;
    }

    public function get_error() {
        return ($this->_last_error = !isset($this->_response->error) || is_null($this->_response->error) ? null : $this->_response->error);
    }

}

?>
